package com.cs2.volunteerpostbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cs2.volunteerpostbackend.dao.CommentMapper;
import com.cs2.volunteerpostbackend.po.Comment;
import com.cs2.volunteerpostbackend.service.CommentService;
import com.cs2.volunteerpostbackend.web.dto.comment.AddComment;
import com.cs2.volunteerpostbackend.web.vo.comment.CommentByPage;
import com.cs2.volunteerpostbackend.web.vo.comment.User2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

@Service
public class CommerntServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public boolean addComment(AddComment addComment) {
        Comment comment = new Comment();
        comment.setMomentId(addComment.getMomentId());
        comment.setCommenterId(addComment.getCommenterId());
        comment.setContent(addComment.getContent());
        comment.setCreateTime(System.currentTimeMillis());

        return this.save(comment);

    }

    @Override
    public boolean delComment(String id) {

        Comment comment = new Comment();
        comment.setDeleted("0");

        UpdateWrapper<Comment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);

        return commentMapper.update(comment, updateWrapper) > 0;
    }

    @Override
    public List<CommentByPage> getCommentPage(String momentId, int page, int size) {
        List<CommentByPage> list = commentMapper.getCommentPage(momentId, (page-1)*size, size);
        for (CommentByPage page1 : list) {
            page1.setUserinfo(new User2(page1.getuId(), page1.getuName(), page1.getuAcount(), page1.getuLocation()));
        }
        return list;
    }
}
